﻿using System;
using NUnit.Framework;
using System.Web.Mvc;
using ProjetoComperio.SystemFramework.Web;

namespace ProjetoComperio.Test.SystemFramework.Web
{
    class MyClass
    {
        public String MyStringProperty { get; set; }
        public int MyIntProperty { get; set; }
        public Boolean MyBooleanProperty { get; set; }
        public Boolean MyAdditionalProperty { get; set; }


    }

    [TestFixture]
    public class ComperioControllerTest : ComperioController
    {
        FormCollection form;

        [SetUp]
        public void SetUp()
        {
            form = new FormCollection();
            form.Add("MyStringProperty", "MyStringPropertyValue");
            form.Add("MyIntProperty", "1");
            form.Add("MyBooleanProperty", "True");

        }

        [Test]
        public void FillEntityTest()
        {
            MyClass myClass = new MyClass();
            this.FillEntity(myClass, form);

            Assert.AreEqual(form["MyStringProperty"], myClass.MyStringProperty);
            Assert.AreEqual(form["MyIntProperty"], myClass.MyIntProperty.ToString());
            Assert.AreEqual(form["MyBooleanProperty"], myClass.MyBooleanProperty.ToString());
        }

        [Test]
        public void FillEntityTestMoreFormKeysThanProps()
        {
            form.Add("AdditionalKey1", "AdditionalKey1Value");
            form.Add("AdditionalKey2", "AdditionalKey2Value");

            MyClass myClass = new MyClass();
            this.FillEntity(myClass, form);

            Assert.AreEqual(form["MyStringProperty"], myClass.MyStringProperty);
            Assert.AreEqual(form["MyIntProperty"], myClass.MyIntProperty.ToString());
            Assert.AreEqual(form["MyBooleanProperty"], myClass.MyBooleanProperty.ToString());
        }

        [Test]
        public void FillEntityTestUsingOtherObject()
        {
            this.FillEntity(new Object(), form);
        }

        [Test]
        [ExpectedException(typeof(NullReferenceException))]
        public void FillEntityTestNullForm()
        {
            MyClass myClass = new MyClass();
            this.FillEntity(myClass, null);
        }

        [Test]
        [ExpectedException(typeof(NullReferenceException))]
        public void FillEntityTestNullObject()
        {
            MyClass myClass = new MyClass();
            this.FillEntity(null, form);
        }
    }
}
